NAMESPACE_ENTER(CFX)

#include "ReShade/ioFX.cfg"

#if USE_HighPassSharpening == 1
//High Pass Sharpening for ReShade + Framework by Ioxa
//Version 1.0
/* This shader uses the gaussian blur passes from Boulotaur2024's gaussian blur/ bloom, unsharpmask shader which are based
   on the implementation in the article "Efficient Gaussian blur with linear sampling"
   http://rastergrid.com/blog/2010/09/efficient-gaussian-blur-with-linear-sampling/ .
   The blend modes are based on algorithms found at http://www.dunnbypaul.net/blends/ , 
   http://www.deepskycolors.com/archive/2010/04/21/formulas-for-Photoshop-blending-modes.html ,
   http://www.simplefilter.de/en/basics/mixmods.html and http://en.wikipedia.org/wiki/Blend_modes . 
   For more info go to http://reshade.me/forum/shader-presentation/529-high-pass-sharpening */ 
#define CoefLuma_HP float3(0.2126, 0.7152, 0.0722)

float4 SharpBlurFinal(in float4 pos : SV_Position, in float2 texcoord : TEXCOORD) : COLOR
{
	#define PX_SIZE (RFX_pixelSize/SharpPrecision)
	float4 color = tex2D(RFX_backbufferColor, texcoord);
	
	#if SharpRadius == 2
	
		color += tex2D(RFX_backbufferColor, texcoord + float2(0.0, 3*PX_SIZE.y))*0.04167;
		color += tex2D(RFX_backbufferColor, texcoord - float2(0.0, 3*PX_SIZE.y))*0.04167;
		color += tex2D(RFX_backbufferColor, texcoord + float2( 3*PX_SIZE.x, 0.0))*0.04167;
		color += tex2D(RFX_backbufferColor, texcoord - float2( 3*PX_SIZE.x, 0.0))*0.04167;
	
		color += tex2D(RFX_backbufferColor, texcoord + float2( 3*PX_SIZE.x, PX_SIZE.y))*0.04167;
		color += tex2D(RFX_backbufferColor, texcoord - float2( 3*PX_SIZE.x, PX_SIZE.y))*0.04167;
		color += tex2D(RFX_backbufferColor, texcoord + float2( -3*PX_SIZE.x, PX_SIZE.y))*0.04167;
		color += tex2D(RFX_backbufferColor, texcoord + float2( 3*PX_SIZE.x, -PX_SIZE.y))*0.04167;
		
		color += tex2D(RFX_backbufferColor, texcoord + float2( 3*PX_SIZE.x, 2*PX_SIZE.y))*0.04167;
		color += tex2D(RFX_backbufferColor, texcoord - float2( 3*PX_SIZE.x, 2*PX_SIZE.y))*0.04167;
		color += tex2D(RFX_backbufferColor, texcoord + float2( -3*PX_SIZE.x, 2*PX_SIZE.y))*0.04167;
		color += tex2D(RFX_backbufferColor, texcoord + float2( 3*PX_SIZE.x, -2*PX_SIZE.y))*0.04167;		
		
		color += tex2D(RFX_backbufferColor, texcoord + float2( PX_SIZE.x, 3*PX_SIZE.y))*0.04167;
		color += tex2D(RFX_backbufferColor, texcoord - float2( PX_SIZE.x, 3*PX_SIZE.y))*0.04167;
		color += tex2D(RFX_backbufferColor, texcoord + float2( -PX_SIZE.x, 3*PX_SIZE.y))*0.04167;
		color += tex2D(RFX_backbufferColor, texcoord + float2( PX_SIZE.x, -3*PX_SIZE.y))*0.04167;

		color += tex2D(RFX_backbufferColor, texcoord + float2( 2*PX_SIZE.x, 3*PX_SIZE.y))*0.04167;
		color += tex2D(RFX_backbufferColor, texcoord - float2( 2*PX_SIZE.x, 3*PX_SIZE.y))*0.04167;
		color += tex2D(RFX_backbufferColor, texcoord + float2( -2*PX_SIZE.x, 3*PX_SIZE.y))*0.04167;
		color += tex2D(RFX_backbufferColor, texcoord + float2( 2*PX_SIZE.x, -3*PX_SIZE.y))*0.04167;
		
		color += tex2D(RFX_backbufferColor, texcoord + float2( 3*PX_SIZE.x, 3*PX_SIZE.y))*0.04167;
		color += tex2D(RFX_backbufferColor, texcoord - float2( 3*PX_SIZE.x, 3*PX_SIZE.y))*0.04167;
		color += tex2D(RFX_backbufferColor, texcoord + float2( -3*PX_SIZE.x, 3*PX_SIZE.y))*0.04167;
		color += tex2D(RFX_backbufferColor, texcoord + float2( 3*PX_SIZE.x, -3*PX_SIZE.y))*0.04167;
		
		color *= 0.5; 
	#endif
	
	#if SharpRadius >= 1
	
		color += tex2D(RFX_backbufferColor, texcoord + float2(0.0, 2*PX_SIZE.y))*0.0625;
		color += tex2D(RFX_backbufferColor, texcoord - float2(0.0, 2*PX_SIZE.y))*0.0625;
		color += tex2D(RFX_backbufferColor, texcoord + float2( 2*PX_SIZE.x, 0.0))*0.0625;
		color += tex2D(RFX_backbufferColor, texcoord - float2( 2*PX_SIZE.x, 0.0))*0.0625;
	
		color += tex2D(RFX_backbufferColor, texcoord + float2( 2*PX_SIZE.x, PX_SIZE.y))*0.0625;
		color += tex2D(RFX_backbufferColor, texcoord - float2( 2*PX_SIZE.x, PX_SIZE.y))*0.0625;
		color += tex2D(RFX_backbufferColor, texcoord + float2( -2*PX_SIZE.x, PX_SIZE.y))*0.0625;
		color += tex2D(RFX_backbufferColor, texcoord + float2( 2*PX_SIZE.x, -PX_SIZE.y))*0.0625;
		
		color += tex2D(RFX_backbufferColor, texcoord + float2( PX_SIZE.x, 2*PX_SIZE.y))*0.0625;
		color += tex2D(RFX_backbufferColor, texcoord - float2( PX_SIZE.x, 2*PX_SIZE.y))*0.0625;
		color += tex2D(RFX_backbufferColor, texcoord + float2( -PX_SIZE.x, 2*PX_SIZE.y))*0.0625;
		color += tex2D(RFX_backbufferColor, texcoord + float2( PX_SIZE.x, -2*PX_SIZE.y))*0.0625;
		
		color += tex2D(RFX_backbufferColor, texcoord + float2( 2*PX_SIZE.x, 2*PX_SIZE.y))*0.0625;
		color += tex2D(RFX_backbufferColor, texcoord - float2( 2*PX_SIZE.x, 2*PX_SIZE.y))*0.0625;
		color += tex2D(RFX_backbufferColor, texcoord + float2( -2*PX_SIZE.x, 2*PX_SIZE.y))*0.0625;
		color += tex2D(RFX_backbufferColor, texcoord + float2( 2*PX_SIZE.x, -2*PX_SIZE.y))*0.0625;
		
		color *= 0.5; 
	#endif 
	
		color += tex2D(RFX_backbufferColor, texcoord + float2(0.0, PX_SIZE.y))*0.125;
		color += tex2D(RFX_backbufferColor, texcoord - float2(0.0, PX_SIZE.y))*0.125;
		color += tex2D(RFX_backbufferColor, texcoord + float2( PX_SIZE.x, 0.0))*0.125;
		color += tex2D(RFX_backbufferColor, texcoord - float2( PX_SIZE.x, 0.0))*0.125;
		color += tex2D(RFX_backbufferColor, texcoord + float2( PX_SIZE.x, PX_SIZE.y))*0.125;
		color += tex2D(RFX_backbufferColor, texcoord - float2( PX_SIZE.x, PX_SIZE.y))*0.125;
		color += tex2D(RFX_backbufferColor, texcoord + float2( -PX_SIZE.x, PX_SIZE.y))*0.125;
		color += tex2D(RFX_backbufferColor, texcoord + float2( PX_SIZE.x, -PX_SIZE.y))*0.125;
		color *= 0.5;
	
	float4 orig = tex2D(RFX_backbufferColor, texcoord); //Original Image
	float3 luma = dot(orig.rgb,CoefLuma_HP);
	float3 chroma = orig.rgb - luma;
	
			float3 sharp = 1 - dot(color.rgb,CoefLuma_HP);
			sharp = (luma + sharp)*0.5;
				
			float3 curve = (2*(sharp - 0.5)*(sharp+0.5))+0.5;
			float3 sharpMin = saturate(lerp(sharp,curve,DarkLineIntensity));
			sharpMin = min(sharp,sharpMin);
			float3 sharpMax = saturate(lerp(sharp,curve,LightLineIntensity));
			sharpMax = max(sharp,sharpMax);
			sharp = 0.5*(sharpMin+sharpMax);
				
			sharp = clamp(sharp,0.50-DarkLineAmount,0.50+LightLineAmount);
			
			#if ViewSharpMask == 1
				orig.rgb = sharp;
				luma = sharp;
			#elif BlendMode == 3
				//Multiply
				sharp = 2 * luma * sharp;
			#elif BlendMode == 6
				//Screen
				sharp = 1.0 - (2*(1.0-luma)*(1.0-sharp));
			#elif BlendMode == 2
				//overlay
				sharp = lerp(2*luma*sharp, 1.0 - 2*(1.0-luma)*(1.0-sharp), step(0.5,luma));
			#elif BlendMode == 4
				//Hardlight
				sharp = lerp(2*luma*sharp, 1.0 - 2*(1.0-luma)*(1.0-sharp), step(0.5,sharp));
			#elif BlendMode == 1 
				//softlight
				sharp = lerp(2*luma*color.rgb + luma*luma*(1.0-2*color.rgb), 2*luma*(1.0-sharp)+pow(luma,0.5)*(2*sharp-1.0), smoothstep(0.0,0.49,sharp.rgb));
			#elif BlendMode == 5
				//vivid light
				sharp = lerp(1-(1-luma)/(2*sharp), luma/(2*(1-sharp)), step(0.5,sharp));
			#endif
				
		orig.rgb = lerp(luma, sharp, HPSharpStrength);
		
		#if ViewSharpMask == 1
		#else
			orig.rgb += chroma;
		#endif
		
		//orig.rgb = color.rgb;
	return saturate(orig);
}


technique HighPassSharp <bool enabled = RFX_Start_Enabled; int toggle = HighPass_ToggleKey; >
{

	pass Sharp
	{
		VertexShader = RFX_VS_PostProcess;
		PixelShader = SharpBlurFinal;
	}

}
#endif

#include "ReShade/ioFX.undef"

NAMESPACE_LEAVE()

#pragma message "thecoryguy@gmail.com\n\n"
